import { AnyAction } from 'redux';
import * as TYPES from '../action-types';

export interface ProfileState {
  loginState: TYPES.LOGIN_TYPES;
  user: any;
  error: string | null;
}

let initialState: ProfileState = {
  loginState: TYPES.LOGIN_TYPES.UN_VALIDATE,
  user: null,
  error: null,
};

export default function (state: ProfileState = initialState, action: AnyAction): ProfileState {
  switch (action.type) {
    case TYPES.VALIDATE:
      if (action.payload.success) {
        return {
          ...state,
          loginState: TYPES.LOGIN_TYPES.LOGINED,
          user: action.payload.data,
          error: null,
        };
      } else {
        return {
          ...state,
          loginState: TYPES.LOGIN_TYPES.UNLOGIN,
          user: null,
          error: action.payload,
        };
      }
    default:
      return state;
  }
}
